package org.linlinjava.litemall.core.service;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.linlinjava.litemall.core.dto.VendorAllinone;
import org.linlinjava.litemall.core.vo.VendorRegVo;
import org.linlinjava.litemall.db.domain.*;
import org.linlinjava.litemall.db.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class AdminVendorService {
    private final Log logger = LogFactory.getLog(AdminVendorService.class);

    @Autowired
    private VendorService vendorService;

    @Autowired
    private LitemallAdminService adminService;

    public List<Vendor> query(String vendorCode, String vendorName, Integer status, Integer page, Integer limit, String sort, String order) {
        if(status == -100) {
            status = null;
        }
        return vendorService.query(vendorCode, vendorName, status, page, limit, sort, order);
    }

    @Transactional
    public VendorAllinone doReg(VendorRegVo regVo) {
        //商户
        Vendor vendor = new Vendor();
        vendor.setStatus(-1);
        vendorService.add(vendor);

        //雇员表
        VendorEmployee employee = regVo.toVendorEmployee();
        employee.setVendorId(vendor.getId());
        vendorService.addEmployee(employee);

        //登陆账号
        createAdmin(employee);

        VendorAllinone result = new VendorAllinone();
        result.setRegVo(regVo);
        result.setVendor(vendor);
        return result;
    }

    public Vendor getById(Long id) {
        return vendorService.findById(id);
    }

    @Transactional
    public void create(VendorAllinone vendor) {
        Long id = doReg(vendor.getRegVo()).getVendor().getId();
        vendor.getVendor().setId(id);
        vendorService.updateById(vendor.getVendor());
    }

    public Vendor findById(Long id) {
        return vendorService.findById(id);
    }

    @Transactional
    public int updateById(Vendor vendor) {
        //更新头像
        List<LitemallAdmin> admins = adminService.findByVendorId(vendor.getId());
        for(LitemallAdmin admin : admins) {
            admin.setAvatar(vendor.getAvatar());
            adminService.updateById(admin);
        }
        return vendorService.updateById(vendor);
    }

    @Transactional
    public void deleteById(Long id) {
        //删除店员及登录账号
        deleteEmployeeByVendorId(id);

        vendorService.deleteById(id);
    }

    @Transactional
    public int approve(Vendor v) {
        return this.updateById(v);
    }

    public List<VendorEmployee> getEmployees(Long vendorId) {
        return vendorService.getEmployees(vendorId);
    }

    @Transactional
    public void deleteEmployeeById(Long id) {
        vendorService.deleteEmployeeById(id);
    }

    @Transactional
    public void deleteEmployee(VendorEmployee employee) {
        List<LitemallAdmin> admins = adminService.findAdmin(employee.getUsername());
        if(!admins.isEmpty()) {
            adminService.deleteById(admins.get(0).getId());
            //TODO 多个怎么办？
        }

        deleteEmployeeById(employee.getId());
    }
    @Transactional
    public void deleteEmployeeByVendorId(Long vendorId) {
        List<VendorEmployee> employees = vendorService.getEmployees(vendorId);
        for(VendorEmployee e : employees) {
            this.deleteEmployee(e);
        }
    }


    @Transactional
    public void addEmployee(VendorEmployee employee) {
        vendorService.addEmployee(employee);
        if(!adminService.findAdmin(employee.getUsername()).isEmpty()) {
            throw new RuntimeException(employee.getUsername() + "已被注册");
        }

        this.createAdmin(employee);
    }

    //生成管理员用户
    private void createAdmin(VendorEmployee employee) {
        LitemallAdmin admin = new LitemallAdmin();
        admin.setUsername(employee.getUsername());
        admin.setPassword(employee.getPassword());
        admin.setVendorId(employee.getVendorId());
        Long[] roleIds = {Long.valueOf(4)};
        admin.setRoleIds(roleIds);

        adminService.add(admin);
    }

    @Transactional
    public int updateEmployeeById(VendorEmployee employee) {
        return vendorService.updateEmployeeById(employee);
    }

}
